#include "StdAfx.h"
#include "FrameRateCounter.h"


CFrameRateCounter::CFrameRateCounter( unsigned long queueLength )
{
    m_frameRate   = 0.0;
    m_prevTime = 0;

    for( unsigned int i = 0; i < queueLength; i++ )
    {
        // 100 milliseconds
        m_frameTime.push_back( 1.0 );
    }

   ::QueryPerformanceFrequency( (LARGE_INTEGER*)&m_frequency );
   ::QueryPerformanceCounter( (LARGE_INTEGER*)&m_prevTime );


}

CFrameRateCounter::~CFrameRateCounter()
{
}


double CFrameRateCounter::GetFrameRate()
{
    return m_frameRate;
}


void CFrameRateCounter::SetFrameRate( double frameRate )
{
    m_frameRate = frameRate;
}

void CFrameRateCounter::NewFrame()
{
    //DWORD currTime = timeGetTime();
    __int64 currTime;
    ::QueryPerformanceCounter( (LARGE_INTEGER*)&currTime );


    __int64 timeDiff64 = currTime - m_prevTime;
    double  timeDiff = timeDiff64/static_cast<double>(m_frequency);
    if ( timeDiff != 0 )
    {
#if 0
        long queueSize = (unsigned long)(1.0/timeDiff) + 1;
        float absVal = (float)abs( (long)(queueSize - m_frameTime.size()));
        if( (absVal/(float)m_frameTime.size() ) > 0.1 )
        {
            // update queue size due to framerate change.
            for( int i = 0; i < (abs( (long)(queueSize - m_frameTime.size()))); i++ )
            {
                if( queueSize > m_frameTime.size() )
                {
                    // add items
                    m_frameTime.push_back( timeDiff );
                }
                else
                {
                    // dump items
                    m_frameTime.pop_front();
                }
            }
        }
#endif
        m_frameTime.pop_front();
        m_frameTime.push_back( timeDiff );
        double totalTime = 0.0;
        for( unsigned int i = 0; i < m_frameTime.size(); i++ )
        {
            totalTime += m_frameTime[i];
        }

        m_frameRate = 1/(totalTime/m_frameTime.size());

        m_prevTime = currTime;
    }
}
